/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

var Tile = new Class({
  initialize: function(id, x, y, type, selected)
  {
    this.ID = id;
    this.X = x;
    this.Y = y;
    this.Type = type;
    this.Selected = selected || false;

    this.ClassName = "Tile";
  }
});

var Sector = new Class({
  initialize: function(name, map, tiles, buildings, planets, starbases, backgrounds)
  {
    this.Name = name || "";
    this.Map = map || "";
    this.Tiles = tiles || [];
    this.Buildings = buildings || [];
    this.Planets = planets || [];
    this.Starbases = starbases || [];
    this.Backgrounds = backgrounds || [];

    this.ClassName = "Sector";
  },
  Export: function()
  {
    return ExportModule.ExportSector(this);
  },
  Import: function(code)
  {
    var regex = new RegExp('"version":([0-9]*)', "i");
    var version = regex.exec(code)[1].toInt();
    return ImportModules[version].ImportSector(code);
  },
  ToHtml: function()
  {
    var html = "<table class='map' oncontextmenu='return false;'><tr><th></th>";

    var width = this.Tiles[this.Tiles.length - 1].X + 1;  // Last element is the extent
    var height = this.Tiles[this.Tiles.length - 1].Y + 1;

    for (var x = 0; x < width; x++)
      html += "<th width='32px'><img src='./images/blank.jpg' height='0px' width='32px' /><br />" + x + "</th>";

    var id = 0;

    for (var y = 0; y < height; y++)
    {
      row = "<tr><th valign='middle'><img src='./images/blank.jpg' height='32px' width='0px' style='float: left;' />" + y + "</th>";

      for (var x = 0; x < width; x++)
      {
        var tile = this.Tiles[id];
        row += "<td class='" + TileTypeToAbbr(tile.Type) + "' ";

        Global.CurrentTile = { html: row, x: x, y: y, id: id, type: tile.Type };
        EventSink.fireEvent("TileCreated");
        row = Global.CurrentTile.html;

        row += "</td>";
        id++;
      }

      row += "</tr>";
      html += row;
    }

    return html;
  }
});